package com.sendpo.zjx.sendpo.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.Userbankcard;
import com.sendpo.zjx.sendpo.domain.UserbankcardDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/24.
 */

public class BankManagerActivity extends AppCompatActivity {
    @BindView(value = R.id.recyclerview)
    public RecyclerView recyclerview;
    private LinearLayoutManager lm;
    private RecylcerAdaper adpater;
    private int type=0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bankmanager);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }

    private void initComponent() {
        type= getIntent().getIntExtra("type",0);
        lm=new LinearLayoutManager(this, OrientationHelper.VERTICAL,false);
        recyclerview.setLayoutManager(lm);
    }
    @OnClick(value = {R.id.backbt, R.id.add_bt})
    public void onMyClick(View view){
        switch (view.getId()){
            case R.id.backbt:
                finish();
                break;
            case R.id.add_bt:
                Tools.redirectActivity(this,AddBankActivity.class);
                break;
        }
    }
    public class RecylcerAdaper extends  RecyclerView.Adapter<RecyclerView.ViewHolder>{
        public List<Userbankcard> domains;
        public LayoutInflater inflater;
        public RecylcerAdaper(List<Userbankcard>  domains, Context context){
            inflater=LayoutInflater.from(context);
            this.domains=domains;
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            RecyclerView.ViewHolder ih=null;
            ih=new IndexHoler(inflater.inflate(R.layout.layout_bank_listitem,parent,false));
            return ih;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            IndexHoler ih = (IndexHoler) holder;
            final Userbankcard userbankcard=domains.get(position);
            ih.name.setText(userbankcard.getUserbankcardBankname());
            ih.type.setText(userbankcard.getUserbankcardType());
            ih.afteridcard.setText(userbankcard.getUserbankcardBankcard().substring(userbankcard.getUserbankcardBankcard().length()-4,userbankcard.getUserbankcardBankcard().length()));
            ih.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(type==103){
                        Intent data=new Intent();
                        data.putExtra("data",userbankcard);
                        setResult(RESULT_OK,data);
                        finish();
                    }
                }
            });
            ih.rootView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    AlertDialog dialog=new AlertDialog.Builder(BankManagerActivity.this)
                            .setNegativeButton("删除", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                    deletepostion(position);
                                    delete(userbankcard.getUserbankcardId()+"");
                                }
                            }).create();

                    dialog.show();
                    return false;
                }
            });
        }

        @Override
        public int getItemCount() {
            return domains.size();
        }
        public class IndexHoler extends RecyclerView.ViewHolder{
            private TextView afteridcard;
            public View rootView;
            public TextView name;
            public TextView type;
            public IndexHoler(View itemView) {
                super(itemView);
                rootView=itemView;
                name=itemView.findViewById(R.id.name);
                type=itemView.findViewById(R.id.type);
                afteridcard=itemView.findViewById(R.id.afteridcard);
            }
        }

        public void  refresh(List<Userbankcard> list){
            domains.removeAll(this.domains);
            domains.addAll(list);
            notifyDataSetChanged();
        }
        public void deletepostion(int position){
            domains.remove(position);
            notifyDataSetChanged();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        queryAll();
    }

    public void queryAll(){
        final List<User> userList= Xutils.queryAll(getApplication(),User.class);
        if(userList==null||userList.size()==0){
            return;
        }
        User user=userList.get(0);
        Map<String,String> map=new HashMap();
        map.put("userId",user.getUserId()+"");
        Xutils.POST(HttpUrl.queryuserbank, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                UserbankcardDomain userbankcardDomain=gson.fromJson(result, UserbankcardDomain.class);
                switch (userbankcardDomain.getState()){
                    case HttpCode.SUCCESS:

                        if(userbankcardDomain.getData()==null||userbankcardDomain.getData().size()==0){
                            return;
                        }
                        if(adpater==null){
                            recyclerview.setAdapter(adpater=new RecylcerAdaper(userbankcardDomain.getData(),BankManagerActivity.this));
                        }else{
                            adpater.refresh(userbankcardDomain.getData());
                        }
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void delete(String userbankcardId){
        Map<String,String> map=new HashMap();
        map.put("userbankcardId",userbankcardId);
        Xutils.POST(HttpUrl.deletebank, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("***********************"+result);
                Gson gson=new Gson();
                UserbankcardDomain userbankcardDomain=gson.fromJson(result, UserbankcardDomain.class);
                switch (userbankcardDomain.getState()){
                    case HttpCode.SUCCESS:
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
